clearvars -except Globaloption option 
if exist('Globaloption', 'var') == 0
    Globaloption.savefig = 1;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% This code reads in the VAR dynamics and produce the Campbell-Shiller decomposition in the paper
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Load data
if exist('option', 'var') == 0
    option.robust = 4;
end

if option.robust == 0
    loaddata_4CIjoint_annual_cy; % cy * time-varing debt/gdp, output rp=2%
    output_rp = 0.02 - mean(cy);

elseif option.robust == 1
    loaddata_4CIjoint_annual_cy; % cy * time-varing debt/gdp
    output_rp = 0.03 - mean(cy); 

elseif option.robust == 2
    loaddata_4CIjoint_annual_cy_robust; % constant fraction of gdp
    output_rp = 0.03 - mean(cy);

elseif option.robust == 3
    loaddata_4CIjoint_annual_cy_robust; % constant fraction of gdp, output rp=2%
    output_rp = 0.02 - mean(cy);

elseif option.robust == 4
    loaddata_4CIjoint_annual_cy_partial; % cy * time-varing debt/gdp, cy after 1794
    output_rp = 0.03 - mean(cy); 

elseif option.robust == 5
    loaddata_4CIjoint_annual_cy_partial; % cy * time-varing debt/gdp, cy after 1794 , output rp=2%
    output_rp = 0.02 - mean(cy);

elseif option.robust == 6
    loaddata_4CIjoint_annual_cy_robust_partial; % cy after 1794, constant fraction of gdp
    output_rp = 0.03 - mean(cy);

elseif option.robust == 7
    loaddata_4CIjoint_annual_cy_robust_partial; % cy after 1794, constant fraction of gdp, output rp=2%
    output_rp = 0.02 - mean(cy);

elseif option.robust == 8
    loaddata_4CIjoint_annual_cy_robust_partial; % cy after 1794,  constant fraction of gdp, output rp=0.15
    output_rp = 0.015 - mean(cy);

elseif option.robust == 9
    loaddata_4CIjoint_annual_cy_partial_4times; % 4 times convenience yield
    output_rp = 0.03 - mean(cy);
end


ttime = [startdatenum:enddatenum];
filename = ['MAT/cy_UK_case', num2str(option.robust)];

%% Estimation
run ../../tools/cs_estimation;

%% steady-state upper bound calculation, taxrevgdp includes the seignorage revenue
upper = exp(pxbar) * (mean(taxrevgdp - spendgdp));

%% bootstrap
run ../../tools/BS_compute;
save(filename, 's', 'std_coeff', 'gdebt', 'upper', 'pxbar', 'pdT', 'pdG', 'taxrevgdp', 'spendgdp', 'seig_rev');


